﻿#include "HeroUpLayer.h"
#include "utils/MyStringUtils.h"
using namespace std;
using namespace cocos2d;
using namespace cocos2d::ui;//UI命名空间
HeroUpLayer::HeroUpLayer() {

}

HeroUpLayer* HeroUpLayer::create() {
	HeroUpLayer *ret = new (std::nothrow)HeroUpLayer();
	if (ret && ret->init()) {
		ret->autorelease();
		return ret;
	} else {
		CC_SAFE_DELETE(ret);
		return nullptr;
	}
}

bool HeroUpLayer::init() {
	if (!Layer::init()) {
		return false;
	}
	initCSBFile();
	initUserDefault();
	return true;
}

void HeroUpLayer::onExit() {
	Layer::onExit();
	UserDefault::getInstance()->setIntegerForKey("shesu_text", shesuText);
	UserDefault::getInstance()->setIntegerForKey("huoli_text", huoliText);

	UserDefault::getInstance()->setIntegerForKey("shesu_value", shesuValue);
	UserDefault::getInstance()->setIntegerForKey("huoli_value", huoliValue);

	UserDefault::getInstance()->setIntegerForKey("shesuUpCoinNum", shesuUpCoinNum);
	UserDefault::getInstance()->setIntegerForKey("huoliUpCoinNum", huoliUpCoinNum);
}

void HeroUpLayer::initCSBFile() {
	rootNode = CSLoader::createNode("scenes/start/layer/heroUpLayer.csb");
	this->addChild(rootNode);

	shesu_text = dynamic_cast<Text*>(rootNode->getChildByName("shesu_text"));
	huoli_text = dynamic_cast<Text*>(rootNode->getChildByName("huoli_text"));

	shesu_value = dynamic_cast<TextAtlas*>(rootNode->getChildByName("shesu_value"));
	huoli_value = dynamic_cast<TextAtlas*>(rootNode->getChildByName("huoli_value"));

	shesu_UpCoinNum = dynamic_cast<Text*>(rootNode->getChildByName("shesuUpCoinNum"));
	huoli_UpCoinNum = dynamic_cast<Text*>(rootNode->getChildByName("huoliUpCoinNum"));

	shesuUpbtn = dynamic_cast<Button*>(rootNode->getChildByName("shesuUpbtn"));
	shesuUpbtn->addClickEventListener([this](Ref*) {
		//射速升级逻辑
		_eventDispatcher->dispatchCustomEvent("onShesuUp", this);
		addShesuCoin();
		addShesuLevelAndValue();
	});

	huoliUpbtn = dynamic_cast<Button*>(rootNode->getChildByName("huoliUpbtn"));
	huoliUpbtn->addClickEventListener([this](Ref*) {
		//火力升级逻辑
		_eventDispatcher->dispatchCustomEvent("onHuoliUp", this);
		addHuoliCoin();
		addHuoliLevelAndValue();
	});
}

/*
	加载用户数据
*/
void HeroUpLayer::initUserDefault() {
	char filename[10];
	shesuText = UserDefault::getInstance()->getIntegerForKey("shesu_text", 1);
	huoliText = UserDefault::getInstance()->getIntegerForKey("huoli_text", 1);
	sprintf_s(filename, "[Lv %d]", shesuText);
	shesu_text->setString(filename);
	sprintf_s(filename, "[Lv %d]", huoliText);
	huoli_text->setString(filename);

	shesuValue = UserDefault::getInstance()->getIntegerForKey("shesu_value", 10);
	huoliValue = UserDefault::getInstance()->getIntegerForKey("huoli_value", 10);
	shesu_value->setString(to_string(shesuValue));
	huoli_value->setString(to_string(huoliValue));

	shesuUpCoinNum = UserDefault::getInstance()->getIntegerForKey("shesuUpCoinNum", 50);
	huoliUpCoinNum = UserDefault::getInstance()->getIntegerForKey("huoliUpCoinNum", 50);
	shesu_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(shesuUpCoinNum));
	huoli_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(huoliUpCoinNum));
}

void HeroUpLayer::addShesuCoin() {
	shesuUpCoinNum += 100;
	shesu_UpCoinNum->setString(MyStringUtils::getInstance()->coinToString(shesuUpCoinNum));

}

void HeroUpLayer::addHuoliCoin() {
	huoliUpCoinNum += 100;
	huoli_UpCoinNum->setString(MyStringUtils::getInstance()->MyStringUtils::getInstance()->coinToString(huoliUpCoinNum));
}

void HeroUpLayer::addShesuLevelAndValue() {
	char filename[10];
	shesuText += 1;
	sprintf_s(filename, "[Lv %d]", shesuText);
	shesu_text->setString(filename);

	shesuValue += 1;
	shesu_value->setString(MyStringUtils::getInstance()->coinToString(shesuValue));
}

void HeroUpLayer::addHuoliLevelAndValue() {
	char filename[10];
	huoliText += 1;
	sprintf_s(filename, "[Lv %d]", huoliText);
	huoli_text->setString(filename);

	huoliValue += 1;
	huoli_value->setString(MyStringUtils::getInstance()->coinToString(huoliValue));
}